import { Injectable } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import { UserService } from '@/modules/user/user.service';
import { ConfigService } from '@nestjs/config';
import { User } from '@/modules/user/user.entity';

@Injectable()
export class AuthService {
  constructor(
    private readonly userService: UserService,
    private readonly jwtService: JwtService,
    private readonly configService: ConfigService,
  ) {}

  createToken(user: Partial<User>) {
    return this.jwtService.sign(user);
  }
  async login(user: Partial<User>) {
    // 调用userService 的login方法实现登录
    const data = await this.userService.login(user);
    // 对登录加签处理 签名是用户传递过来的字段
    const token = this.createToken({
      id: data.id,
      name: data.name,
      email: data.email,
      role: data.role,
    });
    // 返回加签后的数据
    return Object.assign(data, { token });
  }
}
